﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OudotOliotSMLiigassa
{
    class BLLogic
    {
        private List<string> leagues;
        private List<Player> players;
        private List<int> runningID;
        private List<int> queueID;
        private int currentID;
        private string fileName;

        public BLLogic()
        {
            initialize();
        }

        private void initialize()
        {
            leagues = new List<string>();
            players = new List<Player>();
            runningID = new List<int>();
            queueID = new List<int>();


            leagues.Add("Jokerit");
            leagues.Add("Kalpa");
            leagues.Add("Kärpät");
            leagues.Add("Tappara");
            leagues.Add("JYP");
            leagues.Add("HIFK");
            leagues.Add("Lukko");
            leagues.Add("HPK");
            leagues.Add("SaiPa");
            leagues.Add("Blues");
            leagues.Add("Ässät");
            leagues.Add("Pelicans");
            leagues.Add("TPS");
            leagues.Add("Ilves");

            this.runningID.Add(0);
            this.currentID = 0;
            this.fileName = "SM_liiga.txt";

        }

        public int getRunID() 
        {
            return this.runningID[runningID.Count -1];
        }

        public Boolean validate(string fName, string lName, string priceText)
        {

            if (fName == "" || lName == "" || priceText == "")
            {
                return false;
            }

            for (int i = 0; i < getPlayerCount(); i++)
            {
                if (players[i].getFName().ToLower() == fName.ToLower() && players[i].getLName().ToLower() == lName.ToLower())
                {
                    return false;
                }
            }

            decimal price;
            if (!(decimal.TryParse(priceText, out price))) return false; 

            return true;
        }

        public Boolean addPlayer(string fName, string lName, string leagueName, string priceText)
        {
            if (!validate(fName, lName, priceText)) return false;
            
            decimal price;
            if (!(decimal.TryParse(priceText, out price))) return false;      

            if (queueID.Count > 0)
            {
                players.Add(new Player(fName, lName, price, leagueName, queueID[0]));
                this.runningID.Add(queueID[0]);
                currentID = queueID[0];
                queueID.RemoveAt(0);
            }
            else
            {
                players.Add(new Player(fName, lName, price, leagueName, getRunID()));
                currentID = runningID[runningID.Count - 1];
                this.runningID.Add(getRunID() + 1);
            }


            return true;
        }

        public void removePlayer(int id)
        {
            if (getIdCount() == 0)
            {
                return;
            }

            for (int i = 0; i < players.Count; i++)
            {
                if (players[i].getID() == id)
                {
                    players.RemoveAt(i);
                    break;
                }
            }

            updateQueueID(id);

        }

        public int getCurrentID()
        {
            return currentID;
        }

        public int getIdCount()
        {
            return this.runningID.Count;
        }

        public int getPlayerCount()
        {
            return this.players.Count;
        }

        public int getPlayerID(int index)
        {
            return this.players[index].getID();
        }

        public int getPlayerIndexByID(int ID)
        {
            for (int i = 0; i < getPlayerCount(); i++)
            {
                if (players[i].getID() == ID) return i;
            }

            return 0;
        }

        public Player getPlayer(int index)
        {
            return players[index];
        }

        public Player getPlayerByID(int ID)
        {
            for (int i = 0; i < getPlayerCount(); i++)
            {
                if (players[i].getID() == ID) return players[i].getPlayer();
            }

            return null;
        }

        public string getFNameByID(int ID)
        {
            Player p = getPlayerByID(ID);
            return p.getFName();
        }

        public string getLNameByID(int ID)
        {
            Player p = getPlayerByID(ID);
            return p.getLName();
        }

        public decimal getPriceByID(int ID)
        {
            Player p = getPlayerByID(ID);
            return p.getPrice();
        }

        public string getLeagueByID(int ID)
        {
            Player p = getPlayerByID(ID);
            return p.getLeagueName();
        }

        public void setFNameByID(int ID, string fName)
        {
            players[getPlayerIndexByID(ID)].setFName(fName);
        }

        public void setLNameByID(int ID, string lName)
        {
            players[getPlayerIndexByID(ID)].setLName(lName);
        }

        public void setPriceByID(int ID, decimal price)
        {
            players[getPlayerIndexByID(ID)].setPrice(price);
        }

        public void setLeagueByID(int ID, string league)
        {
            players[getPlayerIndexByID(ID)].setLeagueName(league);
        }

        private void updateQueueID(int id)
        {
            for (int i = 0; i < runningID.Count; i++)
            {
                if (runningID[i] == id)
                {
                    runningID.RemoveAt(i);
                    break;
                }
            }

            this.queueID.Add(id);

        }

        public string getLeagues(int index)
        {
            return leagues[index];
        }

        public int getLeagueSize()
        {
            return leagues.Count;
        }

        public Boolean ifExists(string fName, string lName)
        {
            for (int i = 0; i < getPlayerCount(); i++)
            {
                if (players[i].getFName() == fName && players[i].getLName() == lName)
                {
                    return true;
                }
            }
            return false;
        }

        public Boolean saveFile()
        {
            try
            {
                System.IO.TextWriter file = new System.IO.StreamWriter(fileName);
                for (int i = 0; i < getPlayerCount(); i++) 
                {
                    file.WriteLine(players[i].getFName() + " " + players[i].getLName() + " " + players[i].getLeagueName() + " " + players[i].getPrice() + " " + players[i].getID() + "\n");
                }

                file.Close();
            }
            catch (System.Exception)
            {
                return false;
            }

            return true;
        }

    }
}
